
package fregat.base.util;

/**
 * Functional-class to convert byte array into numeric (Integer, Double ... ) representation.
 *
 * @author Kovyrov Vadim <br><b><i>ECO_L O G</i> &nbsp Logistiksysteme GmbH</b>
 * @version 1.0
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class Number {
    
    public static byte readByte( byte b[] ) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b))).readByte();
        }
    
    public static byte readByte( byte b[], int o, int l) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b,o,l))).readByte();
        }
    
    public static short readShort( byte b[] ) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b))).readShort();
        }
    
    public static short readShort( byte b[], int o, int l) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b,o,l))).readShort();
        }
    
    public static int readInt( byte b[] ) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b))).readInt();
        }
    
    public static int readInt( byte b[], int o, int l) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b,o,l))).readInt();
        }
    
    public static double readDouble( byte b[] ) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b))).readDouble();
        }
    
    public static double readDouble( byte b[], int o, int l) throws Exception {
        return new DataInputStream( (InputStream)(
                new ByteArrayInputStream(b,o,l))).readDouble();
        }
    
    public static byte[] longToArray( long l ) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream( bos ); dos.writeLong(l);
        return ( bos).toByteArray();
        }
    
    public static byte[] shortToArray( short s ) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream( bos ); dos.writeShort(s);
        return ( bos).toByteArray();
        }
    
    public static byte[] intToArray( int i ) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream( bos ); dos.writeInt(i);
        return ( bos).toByteArray();
        }
    
    }//class

